package com.xujc;
import com.xujc.base.*;//*代表导入所有

public class InfoManager {
    private Student[] students;  //复习数组的知识点
    private Teacher[] teachers;
    private int studentCount = 0;
    private int teacherCount = 0;

    public InfoManager(int maxStudents, int maxTeachers) {
        students = new Student[maxStudents];
        teachers = new Teacher[maxTeachers];
    }

    public void addStudent(Student student) {
        if (studentCount < students.length) {
            students[studentCount] = student;
            studentCount++;
        } else {
            System.out.println("老王提示：内存不足，没法增加了哈.");
        }
    }

    public void addTeacher(Teacher teacher) {
        if (teacherCount < teachers.length) {
            teachers[teacherCount] = teacher;
            teacherCount++;
        } else {
            System.out.println("老王提示：内存不足，没法增加了哈..");
        }
    }

    public Student findStudentByName(String name) {
        for (int i = 0; i < studentCount; i++) {
            if (students[i].getName().equals(name)) {
                return students[i];
            }
        }
        return null;
    }

    public Teacher findTeacherByName(String name) {
        for (int i = 0; i < teacherCount; i++) {
            if (teachers[i].getName().equals(name)) {
                return teachers[i];
            }
        }
        return null;
    }

//    使用了一个循环来遍历学生数组students，其中studentCount表示学生的数量。在每次循环中，
//    通过比较当前学生的姓名与传入的姓名是否相等，来判断是否是要删除的学生。
//    如果找到了要删除的学生，那么会执行另一个循环，将该学生之后的所有学生向前移动一位，覆盖掉要删除的学生。
    public void deleteStudentByName(String name) {
        for (int i = 0; i < studentCount; i++) {
            if (students[i].getName().equals(name)) {
                for (int j = i; j < studentCount - 1; j++) {
                    students[j] = students[j + 1];
                }
                studentCount--;
                break;
            }
        }
    }

    public void deleteTeacherByName(String name) {
        for (int i = 0; i < teacherCount; i++) {
            if (teachers[i].getName().equals(name)) {
                for (int j = i; j < teacherCount - 1; j++) {
                    teachers[j] = teachers[j + 1];
                }
                teacherCount--;
                break;
            }
        }
    }

    public void updateStudent(String name, int age, String grade) {
        for (int i = 0; i < studentCount; i++) {
            if (students[i].getName().equals(name)) {
                students[i].setAge(age);
                students[i].setGrade(grade);
                break;
            }
        }
    }

    public void updateTeacher(String name, int age, String title) {
        for (int i = 0; i < teacherCount; i++) {
            if (teachers[i].getName().equals(name)) {
                teachers[i].setAge(age);
                teachers[i].setTitle(title);
                break;
            }
        }
    }

    public void printAllStudents() {
        for (int i = 0; i < studentCount; i++) {
            System.out.println("Student: " + students[i].getName() + ", Age: " + students[i].getAge() + ", Grade: " + students[i].getGrade());
        }
    }

    //把所有学生数据转为一个字符串
    public String getAllStudentsData() {
        String allData = "";
        for (int i = 0; i < studentCount; i++) {
            allData += "Student: " + students[i].getName() + ", Age: " + students[i].getAge() + ", Grade: " + students[i].getGrade() + "<br> ";

        }
        return "<html>" + allData + "</html>";   //输出为html语言，<br>标签才可以换行
    }

    public void printAllTeachers() {
        for (int i = 0; i < teacherCount; i++) {
            System.out.println("Teacher: " + teachers[i].getName() + ", Age: " + teachers[i].getAge() + ", Title: " + teachers[i].getTitle());
        }
    }
}
